package de.tum.in.eist.poll.adapter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.controller.UserController;


/**
 * Key press listner to allow login by pressing enter
 * @author sudi
 *
 */
class PressEnterListener implements KeyPressHandler {
	LoginAdapter login;
	
	public PressEnterListener(LoginAdapter login) {
		this.login = login;
	}

	public void onKeyPress(KeyPressEvent event) {
		if (event.getCharCode() == KeyCodes.KEY_ENTER) {
			if (login.name.getText().equals("")) {
				return;
			} else if (login.passwd.getText().equals("")) {
				login.passwd.setFocus(true);
			} else {
		//		loginDialog.removeFromParent();
				login.userController.login(login.name.getText(), login.passwd.getText(),login);
			}
		}
	}

}

/**
 * The login view system
 * handles the login view state
 * @author sudi
 *
 */
public class LoginAdapter {
	/**
	 * dialog and buttons and text/-boxes
	 */
	public DialogBox loginDialog = new DialogBox();
	public TextBox name = new TextBox();
	public PasswordTextBox passwd = new PasswordTextBox();
	public Button loginButton = new Button("Login");

	UserController userController;
	
	private LoginAdapter login = this; 

	public LoginAdapter(UserController u) {
		userController = u;

		loginButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (name.getText().equals("")) {
					name.setFocus(true);
				} else if (passwd.getText().equals("")) {
					passwd.setFocus(true);
				} else {
				//	loginDialog.removeFromParent();
					userController.login(name.getText(), passwd.getText(),login);
				}
			}

		});

		name.addKeyPressHandler(new PressEnterListener(this));

		passwd.addKeyPressHandler(new PressEnterListener(this));
	}
	
	/**
	 * create the login view
	 */
	public void create() {
		HTML element = new HTML("EIST Poll");
		element.setStylePrimaryName("headline");
		
		RootPanel.get().add(element);
		
		VerticalPanel panel = new VerticalPanel();

		FlexTable table = new FlexTable();
		table.setText(0, 0, "Name:");
		table.setText(1, 0, "Password:");
		name.setStyleName("loginTextBox");
		passwd.setStyleName("loginTextBox");
		table.setWidget(0, 1, name);
		table.setWidget(1, 1, passwd);
		panel.add(table);

		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.setSpacing(40);
		hpanel.setStyleName("alignCenter");

		hpanel.add(loginButton);
		panel.add(hpanel);

		loginDialog.add(panel);

		loginDialog.center();

		name.setFocus(true);
	}
}
